package com.atguigu.common.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private HashSet<Integer> hashSet = new HashSet<>();
    @Override
    public void initialize(ListValue constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);

        int[] vals = constraintAnnotation.vals();
        for (int val: vals) {
            hashSet.add(val);
        }

    }

    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return hashSet.contains(integer);
    }
}
